//package com.lab303.powerproject.strategy.impl;
//
//import com.lab303.powerproject.entity.HistoryData;
//import com.lab303.powerproject.entity.StrategyOutput;
//import com.lab303.powerproject.strategy.BaseWorkStrategy;
//import com.lab303.powerproject.strategy.WorkStrategyInterface;
//import com.lab303.powerproject.strategy.enums.StrategyEnums;
//import com.lab303.powerproject.strategy.pojo.*;
//import com.lab303.powerproject.strategy.pojo.device.ConsumeDevice;
//import com.lab303.powerproject.strategy.pojo.device.StorageDevice;
//import com.lab303.powerproject.strategy.pojo.device.SupplyDevice;
//import com.lab303.powerproject.strategy.utils.DateUtils;
//import com.lab303.powerproject.strategy.utils.ElectricityPriceUtil;
//import com.lab303.powerproject.strategy.utils.PrintUtils;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.text.ParseException;
//import java.util.*;
//
///**
// * 经济优先策略
// */
//@Component
//public class EconomicPriorityStrategy extends BaseWorkStrategy implements WorkStrategyInterface  {
//    @Override
//    public Map<String, Object> generate(Date date) throws IOException, ParseException {
//        Integer graphId = getGraphId();
//
//        // 获取电价信息
////        ElectricityPriceUtil utils = getElectricityUtil(graphId);
//        ElectricityPriceUtil utils = null;
//        // 获取峰谷平电价
//        double[] prices = utils.minMiddleMaxPrice();
//        // 获取（电价：索引）
//        Map<Double, List<String>> priceListWithIndex = utils.getPriceListWithIndex();
//
//        PrintUtils.printMap(priceListWithIndex);
//
////        // 查询设备
////        List<BaseDevice> devices = getDevice(graphId, 1);
////
////        // 所有的用能设备
////        List<ConsumeDevice> consums = new ArrayList<>();
////        // 所有的供能设备
////        List<SupplyDevice> supplys = new ArrayList<>();
////        // 所有的储能设备
////        List<StorageDevice> storages = new ArrayList<>();
////
////        for (BaseDevice device: devices) {
////            if (device.getType() == 1) {
////                supplys.add(new SupplyDevice(device, electricityPrice.getPriceListSize()));
////            } else if (device.getType() == 2) {
////                storages.add(new StorageDevice(device, electricityPrice.getPriceListSize(), 0.95));
////            } else {
////                consums.add(new ConsumeDevice(device));
////            }
////        }
//
//        // 组建设备信息（通过RelationShipService知晓关系）
//        // 获取PathAndNode
//        Map<String, String> info = getDeviceInfo(graphId);
//        // 节点
//        String nodes = info.get("node");
//        // 路径
//        String path = info.get("edge");
//        // PathAndNode
//        PathAndNode pathAndNode = new PathAndNode(nodes, path);
//
//        /**
//         * 从数据库读设备，然后再进行策略配置
//         */
////        // 绑定所有供能设备到储能设备
////        for (StorageDevice device: storages) {
////            int[] ids = Arrays.stream(pathAndNode.getPathSplit()).filter(item->item.matches(".*?#" + device.getId() + "$")).mapToInt(item->Integer.parseInt(item.split("#")[0])).toArray();
////            // 假设供能到储能只有一条路径
////            device.setRelDevice(supplys.stream().filter(item->item.getId()==ids[0]).findFirst().get());
////        }
////
////        // 获取所有供能设备到用能设备的路径
////        for (ConsumeDevice device: consums) {
////            int[] ids = Arrays.stream(pathAndNode.getPathSplit()).filter(item->item.matches(".*?#" + device.getId() + "$")).mapToInt(item->Integer.parseInt(item.split("#")[0])).toArray();
////            for(int id: ids) {
////                Optional<SupplyDevice> tempSupply = supplys.stream().filter(item->item.getId()==id).findFirst();
////                if (tempSupply.isPresent()) {
////                    device.addSupply(tempSupply.get());
////                }
////                Optional<StorageDevice> tempStorage = storages.stream().filter(item->item.getId()==id).findFirst();
////                if (tempStorage.isPresent()) {
////                    device.addSupply(tempStorage.get());
////                }
////            }
////        }
//
//        // 组建设备信息（以后采取从数据库中读的形式）
//        // 用能设备
//        ConsumeDevice consumeDevice = new ConsumeDevice(207, "末端");
//        // 供能设备
//        SupplyDevice doubleMachine = new SupplyDevice(203, "双工况冷水机组", utils.getPriceListSize());
//        SupplyDevice normalMachine = new SupplyDevice(204, "常温冷水机组", utils.getPriceListSize());
//        SupplyDevice airMachine = new SupplyDevice(200, "空气源热泵机组", utils.getPriceListSize());
//        // 储能设备
//        StorageDevice iceStorage = new StorageDevice(206, "冰蓄能装置", doubleMachine, utils.getPriceListSize(), 0.95);
//        // 绑定供能设备和用能设备
//        consumeDevice.addSupply(doubleMachine, normalMachine, airMachine, iceStorage);
//
//        // 获取明天的日期
//        Calendar tomorrow = DateUtils.plusOrMinusDay(date, 1);
//
//        // 所预测的明日的天气数据（从数据库中读取）
//        DayWeather tomorrowWeather = getForecastWeatherByDate(tomorrow.getTime());
//        if (tomorrowWeather == null) {
//            // 如果获取到的温度数据为空，设置一个默认值
//            tomorrowWeather = new DayWeather();
//
//            tomorrowWeather.setTempavg(28D);
//            tomorrowWeather.setTemphigh(28D);
//        } else {
//            tomorrowWeather.setTempavg();
//        }
//
//        double tem = tomorrowWeather.getTemphigh();
//
//        // 设置基础策略（暂时写死）[2表示储能策略、1表示供能策略]
//        Strategy s1 = new Strategy(2, iceStorage.getStorageInfo(),1, iceStorage);
//        Strategy s2 = new Strategy(1, iceStorage.getSupplyInfo(), 2, iceStorage);
//        Strategy s3 = new Strategy(1, normalMachine.getSupplyInfo(), 3, normalMachine);
//        Strategy s4 = new Strategy(1, doubleMachine.getSupplyInfo(), 4, doubleMachine);
//        Strategy s5 = new Strategy(1, airMachine.getSupplyInfo(), 5, airMachine);
//
//        // 设置组合策略 (暂时写死)
//        List<com.lab303.powerproject.strategy.pojo.StrategyComb> strategyCombList = new ArrayList<>();
//        strategyCombList.addAll(Arrays.asList(
//                new com.lab303.powerproject.strategy.pojo.StrategyComb(s1)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(s2)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(s3)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(s4)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(6, s1, s3)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(7, s1, s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(8, s2, s3)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(9, s2, s4)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(10, s2, s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(11, s3, s4)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(12, s3, s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(13, s4, s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(14, s2, s3, s4)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(15, s2, s3, s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(16, s2, s4, s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(17, s3, s4, s5)
//                ,new com.lab303.powerproject.strategy.pojo.StrategyComb(18, s2, s3, s4, s5)
//        ));
//
//
//        /**
//         * 生成标记数组 and 能量数组
//         * 方案一：不考虑预测的能量值
//         */
//        // 首先判断明天是周末还是节假日还是工作日
//        // holidayStatus == 0 ：工作日
//        // holidayStatus == 1 ：周末
//        // holidayStatus == 2 ：节假日
//        Integer holidayStatus = isHolidayByDate(tomorrow.getTime());
//        if (holidayStatus == 2) {
//            // 节假日时间段无策略
//        } else if (holidayStatus == 1) {
//            // 周末时间段无策略
//        } else {
//            Map<Double, com.lab303.powerproject.strategy.pojo.StrategyComb> combsMap = getStrategyCombMap(tem, prices, strategyCombList, new String[]{"1,8,2","1,14,8","1,18,14"});
//            // 遍历峰谷平电价
//            for (double price: prices) {
//                List<String> dateList = priceListWithIndex.get(price);
//                for (String dateStr: dateList) {
//                    Integer startIndex = Integer.parseInt(dateStr.split("~")[0]);
//                    Integer endIndex = Integer.parseInt(dateStr.split("~")[1]);
//
//                    com.lab303.powerproject.strategy.pojo.StrategyComb comb = combsMap.get(price);
//                    comb.setFlagAllBindingDevice(startIndex, endIndex);
//                }
//            }
//
//        }
//
////        /**
////         * 方案二：考虑预测的能量值
////         */
////        if (holidayStatus == 2) {
////            // 节假日时间段无策略
////        } else if (holidayStatus == 1) {
////            // 周末时间段无策略
////        } else {
////
////        }
//
//        /**
//         * 测试：手动设置各供能/储能设备的flag数组，看策略效果
//         */
//        // iceStorage.setFlag(new int[]{0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,2,2,2});
//        // doubleMachine.setFlag(new int[]{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0});
//        // normalMachine.setFlag(new int[]{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0});
//        // airMachine.setFlag(new int[]{0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0});
//
//        /**
//         * 生成策略列表（目前的策略按照工况表匹配理论值出）
//         */
//        // 冰蓄能
//        Map<Integer, Strategy> iceStorageMapping = new HashMap<>();
//        iceStorageMapping.put(1, s1);
//        iceStorageMapping.put(2, s2);
//        List<StrategyOutput> iceStorageOutput = getStrategyOutList(iceStorage, date, iceStorageMapping, StrategyEnums.ECONOMIC_TYPE.getType());
//        // 双工况冷水机组
//        Map<Integer, Strategy> doubleMachineMapping = new HashMap<>();
//        doubleMachineMapping.put(1, s4);
//        List<StrategyOutput> doubleMachineOutput = getStrategyOutList(doubleMachine, date, doubleMachineMapping,StrategyEnums.ECONOMIC_TYPE.getType());
//        // 常温冷水机组
//        Map<Integer, Strategy> normalMachineMapping = new HashMap<>();
//        normalMachineMapping.put(1, s3);
//        List<StrategyOutput> normalMachineOutput = getStrategyOutList(normalMachine, date, normalMachineMapping,StrategyEnums.ECONOMIC_TYPE.getType());
//        // 空气源热泵机组
//        Map<Integer, Strategy> airMachineMapping = new HashMap<>();
//        airMachineMapping.put(1, s5);
//        List<StrategyOutput> airMachineOutput = getStrategyOutList(airMachine, date, airMachineMapping,StrategyEnums.ECONOMIC_TYPE.getType());
//
//        /**
//         * 组合策略列表
//         */
//        List<StrategyOutput> strategyOutputList = new ArrayList<>();
//        strategyOutputList.addAll(iceStorageOutput);
//        strategyOutputList.addAll(doubleMachineOutput);
//        strategyOutputList.addAll(normalMachineOutput);
//        strategyOutputList.addAll(airMachineOutput);
//
//        /**
//         * 去除无关依赖
//         */
//        // 自动去除所有无关依赖
//        removeNodeOrEdgeAllDevice(pathAndNode, consumeDevice, doubleMachine, normalMachine, airMachine, iceStorage);
//
//        /**
//         * 组装结果
//         */
//        Map<String, Object> ans = new HashMap();
//        ans.put("path", pathAndNode.getPathSplit());
//        ans.put("node", pathAndNode.getNodeSplit());
//
////        // 使用固定path和node测试问题
////        PathAndNode ss = new PathAndNode("203,207", "203#207");
////        ans.put("path", ss.getPathSplit());
////        ans.put("node", ss.getNodeSplit());
//
//        ans.put("strategyOutput", strategyOutputList);
//        return ans;
//    }
//
//    @Override
//    public void save(Date date) throws IOException,ParseException {
//        Map<String, Object> economic = generate(date);
//        List<StrategyOutput> strategyOutputs = (List<StrategyOutput>) economic.get("strategyOutput");
//        saveAllStrategy(strategyOutputs);
//
//        // 组装HistoryData
//        List<HistoryData> historyDataList = new ArrayList<>();
//        // 第二天0点
//        Date tomorrow = DateUtils.getDateWithHour(DateUtils.plusOrMinusDay(date, 1).getTime(), 0).getTime();
//        String[] node = (String[]) economic.get("node");
//        String[] path = (String[]) economic.get("path");
//        historyDataList.add(new HistoryData(tomorrow, String.join(",", node), "node", StrategyEnums.ECONOMIC_TYPE.getType()));
//        historyDataList.add(new HistoryData(tomorrow, String.join(",", path), "path", StrategyEnums.ECONOMIC_TYPE.getType()));
//        saveAllHistoryData(historyDataList);
//    }
//}